package com.company.controller;

import com.company.mapper.EmployeeMapper;
import com.company.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/employee/delete")
public class EmployeeDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 1. 获取要删除的员工ID
        int id = Integer.parseInt(request.getParameter("id"));  // 修正：Integer 首字母大写

// 2. 调用MyBatis执行删除操作
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
        employeeMapper.deleteEmployee(id); // 调用MyBatis的Mapper接口
        MyBatisUtil.closeSqlSession(sqlSession);

        // 3. 重定向回列表页并提示结果
        response.sendRedirect(request.getContextPath() + "/employee/list?message=删除成功");
    }
}